/*****************************************************************************
 * Copyright (c) 2010 -, Anders All rights reserved
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Contact: Anders Uddenberg (anders.uddenberg@gmail.com)
 * 
 *
 ****************************************************************************/
package org.easypr.organizer.server;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.easypr.organizer.client.MyUserService;
import org.easypr.organizer.shared.AppConstants;
import org.easypr.organizer.shared.MyUser;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

// TODO: Auto-generated Javadoc
/**
 * The Class MyUserServiceImpl.
 */
@SuppressWarnings("serial")
public class MyUserServiceImpl extends RemoteServiceServlet implements MyUserService {

	/** The request. */
	HttpServletRequest request;
	
	/** The session. */
	HttpSession session;
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.MyUserService#getLoginURL(java.lang.String)
	 */
	public String getLoginURL(String returnURL) {
		UserService userService = UserServiceFactory.getUserService();
        return userService.createLoginURL(returnURL); 
	}


	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.MyUserService#logout()
	 */
	public MyUser logout() {
		if(request == null) {
			request = this.getThreadLocalRequest();
			session = request.getSession();
		}
		if(session.getAttribute("User") != null) {
			MyUser user = (MyUser)session.getAttribute("User");
			session.removeAttribute("User");
			return user;
		}
		return null;
	}
    
    /* (non-Javadoc)
     * @see org.easypr.organizer.client.MyUserService#getUser()
     */
    public MyUser getUser() {

    	request = this.getThreadLocalRequest();
    	// First check if we have the user in session
    	if(session == null) {
			session = request.getSession();
		} 
		if(session != null) {
			if(session.getAttribute("User")!= null) {
				// We had him in the session, return him
				return (MyUser)session.getAttribute("User");
			} else {
				MyUser user = null;
				// We didn't have the user in session.
				// Proceed to see if the user is logged in as a Google User
				UserService userService = UserServiceFactory.getUserService();
		        User GoogleUser = userService.getCurrentUser();
		    	
		        if (GoogleUser != null) {
		        	// The user is logged in as a Google User
		        	// Proceed to see if he's a registered user
		        	user = new MyUser("", GoogleUser.getEmail(), GoogleUser.getNickname());
		        	
		        	BackboneServiceImpl backbone = new BackboneServiceImpl();
		        	user = backbone.fetchUser(user);
		        	if(user == null) {
		        		// The user is not registered with us
		        		user = new MyUser(GoogleUser.getNickname(), GoogleUser.getEmail(), GoogleUser.getNickname());
		        	} else {
		        		// The user is registered with us
		        		// Lets save him in the session so we save some backbone requests later
			        	if(session == null) {
			        		//System.out.println("System error: Session is null for some reason");
			        		return null;
			        	}
			        	session.setAttribute("User", user);
		        	}
		        	// Set the Google Logout URL
		        	user.setLogoutURL(userService.createLogoutURL(AppConstants.domain));
		        	return user;
		        	
		        } else {
		        	// The user is not logged in as a Google User
		        	user = new MyUser("", "", "");
		        	return user;
		        }
			}
		}
		// We didn't have him in session
		//System.out.println("System error 2: Session is null for some reason");
        return null;
	}
}
